// DDAutoDelete.h: interface 
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_DDAutoDelete_H__INCLUDED_)
#define AFX_DDAutoDelete_H__INCLUDED_

class DDCriticalSection
{
public:
	DDCriticalSection(){::InitializeCriticalSection(&m_sect);}
	~DDCriticalSection(){::DeleteCriticalSection(&m_sect);}

public:
    bool Lock(){ ::EnterCriticalSection(&m_sect); return true; }
	bool Unlock(){ ::LeaveCriticalSection(&m_sect); return true; }

private:
    CRITICAL_SECTION m_sect;
};

struct DD_AUTO_LOCK
{
	DD_AUTO_LOCK(DDCriticalSection* pLock)
	{
		m_pLock = pLock;
		if( NULL != m_pLock )
		{
			m_pLock->Lock();
		}
	}
	~DD_AUTO_LOCK()
	{
		if( NULL != m_pLock )
		{
			m_pLock->Unlock();
		}
	}
	void UnLock()
	{
		if( NULL != m_pLock )
		{
			m_pLock->Unlock();
		}
	}
	void ReLock()
	{
		if( NULL != m_pLock )
		{
			m_pLock->Lock();
		}
	}
	void CancelLock()
	{
		if( NULL != m_pLock )
		{
			m_pLock->Unlock();
			m_pLock = NULL;
		}
	}
	DDCriticalSection* m_pLock;
};


#endif // !defined(AFX_DDAutoDelete_H__INCLUDED_)
